package com.aiju.cash.ui.multiple_shops.bean;

import android.util.SparseIntArray;

import com.aiju.bean.Good;
import com.aiju.bean.GoodsType;
import com.aiju.utils.L;
import com.aiju.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-06-15 10:44
 * @version: V1.0
 * @Description: 关联商品
 */
public class RelationGoodManager {

    //商品列表
    private List<Good> goods = new ArrayList<>();

    //商品分类列表
    private List<GoodsType> goodsTypes = new ArrayList<>();

    //商品分类下 关联商品的数目
    private SparseIntArray selectedNumbers;

    //改动关联的商品列表
    private List<Good> changeGoods = new ArrayList<>();

    //更新视图接口回调
    private List<OnChangeListener> listeners = new ArrayList<>();

    public void addOnChangeListener(OnChangeListener changeView) {
        listeners.add(changeView);
    }

    public interface OnChangeListener {
        void changeView();
    }

    //单例模式获取此对象
    private static volatile RelationGoodManager instance;

    //单例
    public static RelationGoodManager getInstance() {
        if (instance == null) {
            synchronized (RelationGoodManager.class) {
                if (instance == null) {
                    instance = new RelationGoodManager();
                }
            }
        }
        return instance;
    }

    private RelationGoodManager() {
        selectedNumbers = new SparseIntArray();
    }

    /**
     * 初始化商品
     *
     * @param goodsTypes
     */
    public void addGoodData(List<GoodsType> goodsTypes) {
        if (Util.isEmpty(goodsTypes)) {
            return;
        }
        reset();
        this.goodsTypes = goodsTypes;
        for (GoodsType goodsType : goodsTypes) {
            this.goods.addAll(goodsType.getGoodsList());
        }
        for (Good good : goods) {
            if (good.isRelation()) {
                addRelation(good);
            }
        }
    }

    /**
     * 返回当前分类下关联商品个数
     *
     * @param typeId
     * @return
     */
    public int getGoodTypeForRelationSum(int typeId) {
        return selectedNumbers.get(typeId);
    }

    /**
     * 添加关联
     *
     * @param good
     */
    public void addRelation(Good good) {
        int groupCount = selectedNumbers.get(good.getTypeId());
        if (groupCount == 0) {
            selectedNumbers.append(good.getTypeId(), 1);
        } else {
            selectedNumbers.append(good.getTypeId(), ++groupCount);
        }
        good.setCurrentRelation("1");
        if (good.isChange()) {
            if (!changeGoods.contains(good)) {
                changeGoods.add(good);
            }
        } else {
            if (changeGoods.contains(good)) {
                changeGoods.remove(good);
            }
        }
        update();
    }

    /**
     * 移除关联
     *
     * @param good
     */
    public void removeRelation(Good good) {
        int groupCount = selectedNumbers.get(good.getTypeId());
        if (groupCount == 1) {
            selectedNumbers.delete(good.getTypeId());
        } else if (groupCount > 1) {
            selectedNumbers.append(good.getTypeId(), --groupCount);
        }
        good.setCurrentRelation("0");
        if (good.isChange()) {
            if (!changeGoods.contains(good)) {
                changeGoods.add(good);
            }
        } else {
            if (changeGoods.contains(good)) {
                changeGoods.remove(good);
            }
        }
        update();
    }

    /**
     * 刷新数据
     */
    public void update() {
        for (OnChangeListener listener : listeners) {
            listener.changeView();
        }
        L.d("添加关联id：" + getGoodsIds()[0]);
        L.d("移除关联id：" + getGoodsIds()[1]);
        for (GoodsType type : goodsTypes) {
            L.d(type.getName() + "分类关联商品个数：" + getGoodTypeForRelationSum(type.getIdInt()) + "/" + type.getGoodsList().size());
        }
        L.d("============   ===========");
    }

    /**
     * 获取全部关联、移除关联商品 id
     *
     * @return
     */
    public String[] getGoodsIds() {
        StringBuilder save = new StringBuilder();
        StringBuilder delete = new StringBuilder();

        for (Good good : changeGoods) {
            if (good.getCurrentRelation().equals("0")) {
                delete.append("," + good.getGoodsTypeId() + ":" + good.getId());
            } else {
                save.append("," + good.getGoodsTypeId() + ":" + good.getId());
            }
        }
        return new String[]{save.toString().replaceFirst(",", ""), delete.toString().replaceFirst(",", "")};
    }

    /**
     * 商品管理重置
     */
    public void reset() {
        goods.clear();
        goodsTypes.clear();
        changeGoods.clear();
        selectedNumbers.clear();
    }

    public List<Good> getGoods() {
        return goods;
    }

    public void setGoods(List<Good> goods) {
        this.goods = goods;
    }

    public List<GoodsType> getGoodsTypes() {
        return goodsTypes;
    }

    public void setGoodsTypes(List<GoodsType> goodsTypes) {
        this.goodsTypes = goodsTypes;
    }

    public List<Good> getChangeGoods() {
        return changeGoods;
    }

    public void setChangeGoods(List<Good> changeGoods) {
        this.changeGoods = changeGoods;
    }
}
